package main;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import edu.mapi.aamas.common.Task;
import edu.mapi.aamas.common.VehicleState;
import edu.mapi.aamas.gui.tablelayout.TableLayout;
import edu.mapi.aamas.uav.UavAgent;

/**
 * @author ZP
 */
public class VehicleInterface extends JPanel {

	public UavAgent agent;
	
	JLabel xLabel = new JLabel("?");
	JLabel yLabel = new JLabel("?");
	JLabel yawLabel = new JLabel("?");
	DefaultListModel listModel = new DefaultListModel();
	
	public void updateState(VehicleState state) {
		xLabel.setText(""+(int)state.getPoint().x);
		yLabel.setText(""+(int)state.getPoint().y);
		yawLabel.setText(""+(int)Math.toDegrees(state.getYaw())%360);
		listModel.clear();
		for (Task t : agent.getPendantTasks()) {
			listModel.addElement(t);
		}
	}
	
	
	public VehicleInterface(UavAgent agent) {
		this.agent = agent;
		setLayout(new TableLayout(new double[] {TableLayout.FILL}, new double[] {75, TableLayout.FILL}));
		
		JPanel physicalStatePanel = new JPanel(new TableLayout(new double[] {0.33,0.33,0.33}, new double[]{0.5,0.5}));
		physicalStatePanel.setBorder(BorderFactory.createTitledBorder("Physical State"));
		
		physicalStatePanel.add(new JLabel("X:"), "0,0");
		physicalStatePanel.add(new JLabel("Y:"), "1,0");
		physicalStatePanel.add(new JLabel("Yaw:"), "2,0");		
		
		physicalStatePanel.add(xLabel, "0,1");
		physicalStatePanel.add(yLabel, "1,1");
		physicalStatePanel.add(yawLabel, "2,1");		
		
		add(physicalStatePanel, "0,0");
		
		JPanel p2 = new JPanel(new BorderLayout());
		p2.setBorder(BorderFactory.createTitledBorder("Pendant Tasks"));
		p2.add(new JScrollPane(new JList(listModel)), BorderLayout.CENTER);
		add(p2, "0,1");
	}
	
	
	
	public static VehicleInterface showInterface(UavAgent agent) {
		VehicleInterface interf = new VehicleInterface(agent);
		final UavAgent ag = agent;
		JFrame frame = new JFrame("UAV Agent - "+agent.getLocalName());
		frame.getContentPane().add(interf);
		frame.setSize(400, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setVisible(true);
		
		return interf;
	}
}
